using System;
using System.Xml.Serialization;

namespace SharpMud.DynamicTyping
{	
	/// <summary>
	/// This is a class that serves as a means to identify a type that must be dynamically loaded.
	/// This class is abstract.
	/// </summary>
	[XmlRoot("TypeLocation")]
	[XmlInclude(typeof(DynamicTyping.AssembliedTypeLocation))]
	//[XmlInclude(typeof(DynamicTyping.ScriptedType))]
	public abstract class TypeLocation : ITypeLocation
	{
		/// <summary>
		/// The default contructor for the TypeLocation class.
		/// </summary>
		public TypeLocation()
		{
		}

		private string _FullTypeName;

		/// <summary>
		/// When overriden in an derived class, this method returns the type that this
		/// TypeLocation points to.
		/// </summary>
		/// <returns>Returns this TypeLocation's target System.Type</returns>
		public abstract System.Type						AcquireType();
		
		/// <summary>
		/// When overriden in a derived class, this method returns the assembly that contains
		/// the System.Type that this TypeLocation points to.
		/// </summary>
		/// <returns>Returns the assembly that this TypeLocation's target System.Type resides in</returns>
		public abstract System.Reflection.Assembly		AcquireAssembly();

		/// <summary>
		/// This method creates and returns an instance of this TypeLocation's target System.Type
		/// </summary>
		/// <returns>Returns on object instance of this TypeLocation's target System.Type</returns>
		public object AcquireTypeInstance()
		{
			if(AcquireAssembly()==null)
			{
				throw new AssemblyNotFoundException();
			}
			if(AcquireType()==null)
			{
				throw new TypeNotFoundException();
			}
			return AcquireAssembly().CreateInstance(AcquireType().FullName,true);
		}
		
		/// <summary>
		/// This is the full type name of this TypeLocation's target System.Type
		/// </summary>
		[XmlElement("FullTypeName",typeof(string))]
		public string									FullTypeName
		{
			get{return _FullTypeName;	}
			set{_FullTypeName=value;	}
		}

		#region ITypeLocation Members

        /// <summary>
        /// 
        /// </summary>
		string SharpMud.DynamicTyping.ITypeLocation.FullTypeName
		{
			get
			{
				return this.FullTypeName;
			}
		}

		#endregion
	}
}